import json
import os
import serial
'''
保存mqtt连接参数至file_path中，如果文件已存在，则更新参数；如果文件不存在，新建并保存参数
输入：mqPara,file_path
'''
def mq_parm_save(mqPara,file_path):
    # 检查文件是否存在
    if os.path.exists(file_path):
        with open(file_path, 'r') as file:
            existing_data = json.load(file)
        existing_data.update(mqPara)  #文件已存在，更新文件内容
        mqPara = existing_data
    with open(file_path, 'w') as file:  #将数据写入文件
        json.dump(mqPara, file, indent=4)
    return

if __name__ == '__main__':
    msg = {
        "servertype":"control",
        "hostname": "mqtt.ctwing.cn",
        "port": 1883,
        "clientid":"1716701712345678",
        "username": "liuyuye",
        "password": "WdGi3iu7xoEanpYVX7uocBcTvb8-lSkkQO0QOBf1lJA"
        }
    #json_msg = json.dumps(msg, indent=4)
    mq_parm_save(msg,'mq_config2.json')